<template>
  <div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="background: #2f54eb"
          size="small"
          @click="handleAdd"
          v-hasPermi="['report:datasource:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="datasourceList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="数据源名称" align="center" prop="datasourceName"/>
      <el-table-column label="数据源类型" align="center" prop="datasourceType"/>
      <el-table-column label="主机地址" align="center" prop="datasourceHost"/>
      <el-table-column label="端口" align="center" prop="datasourcePort"/>
      <el-table-column label="数据源用户" align="center" prop="datasourceUsername"/>
      <el-table-column label="数据库名称" align="center" prop="datasourceBaseName"/>
      <el-table-column label="数据源描述" align="center" prop="datasourceRemark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['report:datasource:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['report:datasource:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据源信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="数据源名称" prop="datasourceName">
          <el-input v-model="form.datasourceName" placeholder="请输入数据源名称"/>
        </el-form-item>
        <el-form-item label="数据源类型" prop="datasourceType">
          <el-select v-model="form.datasourceType" placeholder="请选择数据源类型">
            <el-option label="mysql" value="mysql"/>
          </el-select>
        </el-form-item>
        <el-form-item label="主机地址" prop="datasourceHost">
          <el-input v-model="form.datasourceHost" placeholder="请输入数据源主机地址"/>
        </el-form-item>
        <el-form-item label="主机端口" prop="datasourcePort">
          <el-input v-model="form.datasourcePort" placeholder="请输入数据源主机端口"/>
        </el-form-item>
        <el-form-item label="数据库名称" prop="datasourceBaseName">
          <el-input v-model="form.datasourceBaseName" placeholder="请输入数据源数据库名称"/>
        </el-form-item>
        <el-form-item label="数据源用户" prop="datasourceUsername">
          <el-input v-model="form.datasourceUsername" placeholder="请输入数据源用户密码"/>
        </el-form-item>
        <el-form-item label="数据源密码" prop="datasourcePassword">
          <el-input v-model="form.datasourcePassword" placeholder="请输入数据源用户密码"/>
        </el-form-item>
        <el-form-item label="数据源链接" prop="datasourceUrl">
          <el-input type="textarea" v-model="form.datasourceUrl" placeholder="请输入数据源链接路径"/>
        </el-form-item>
        <el-form-item label="数据源描述" prop="datasourceRemark">
          <el-input v-model="form.datasourceRemark" placeholder="请输入数据源描述"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listDatasource, getDatasource, delDatasource, addDatasource, updateDatasource} from "@/api/report/datasource";

export default {
  name: "Datasource",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据源信息表格数据
      datasourceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        datasourceName: undefined,
        datasourceUsername: undefined,
        datasourceEnName: undefined,
        datasourceType: undefined,
        datasourceHost: undefined,
        datasourcePort: undefined,
        datasourceInvokes: undefined,
        datasourceIsEnable: '1',
        datasourceBaseName: undefined,
        datasourcePassword: undefined,
        datasourceDriver: undefined,
        datasourceDriverVersion: undefined,
        datasourceUrl: undefined,
        datasourceRemark: undefined,
        createDate: undefined,
        updateDate: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "主键不能为空", trigger: "blur"}
        ], datasourceName: [
          {required: true, message: "数据源名称不能为空", trigger: "blur"}
        ], datasourceType: [
          {required: true, message: "数据源类型不能为空", trigger: "change"}
        ], datasourceHost: [
          {required: true, message: "数据源主机地址不能为空", trigger: "blur"}
        ], datasourcePort: [
          {required: true, message: "数据源主机端口不能为空", trigger: "blur"}
        ], datasourceBaseName: [
          {required: true, message: "数据源数据库名称不能为空", trigger: "blur"}
        ], datasourceUsername: [
          {required: true, message: "数据源用户名称不能为空", trigger: "blur"}
        ], datasourcePassword: [
          {required: true, message: "数据源用户密码不能为空", trigger: "blur"}
        ], datasourceUrl: [
          {required: true, message: "数据源链接路径不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询数据源信息列表 */
    getList() {
      this.loading = true;
      listDatasource(this.queryParams).then(response => {
        this.datasourceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        datasourceName: undefined,
        datasourceUsername: undefined,
        datasourceEnName: undefined,
        datasourceType: undefined,
        datasourceHost: undefined,
        datasourcePort: undefined,
        datasourceInvokes: undefined,
        datasourceIsEnable: undefined,
        datasourceBaseName: undefined,
        datasourcePassword: undefined,
        datasourceDriver: undefined,
        datasourceDriverVersion: undefined,
        datasourceUrl: undefined,
        datasourceRemark: undefined,
        createBy: undefined,
        createDate: undefined,
        updateBy: undefined,
        updateDate: undefined,
        delFlag: undefined,
        org: undefined,
        office: undefined,
        post: undefined
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据源信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getDatasource(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改数据源信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateDatasource(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addDatasource(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据源信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delDatasource(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('report/datasource/export', {
        ...this.queryParams
      }, `datasource_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
